var _formChanged = false;

function allowNumericInputOnly(charValue) 
{
  // Allow only backspace, delete, and tab
  if ( charValue.keyCode == 46 || charValue.keyCode == 8 || charValue.keyCode == 9) 
  {
  } 
  else 
  {
    if (charValue.keyCode < 95) 
    {
      // Ensure that it is a number and stop the keypress
      if (charValue.keyCode < 48 || charValue.keyCode > 57 ) 
      {
      charValue.preventDefault();
      }
    } 
    else 
    {
      // Ensure that it is a number and stop the keypress (keypad)
      if (charValue.keyCode < 96 || charValue.keyCode > 105 ) 
      {
      charValue.preventDefault();
      }
    }
  }
}

function handleFormChanged() 
{
    //$('#save_or_update').attr("disabled", false);
    _formChanged = true;
}

function confirmNavigation() 
{
	if (_formChanged)
		return confirm('Changes have been made to this form and will be lost.\nClick \'ok\' to lose changes.');
	else 
    	 return true;    
}

//Prompt to show if there is a concurrency conflict
function confirmOverwrite() 
{
	return confirm('This record has been changed by another user.\nClick \'Ok\' to overwrite their data.\nClick \'Cancel\' to cancel save and reload changes.');
}

function currencyToNumber(currencyValue) 
{	
	var currency = currencyValue;
	var number = Number(currency.replace(/[^0-9\.]+/g,''));
	return number;
}

function formatToCurrency(amount)
{
	//Must turn var into string to remove $ and , //It doesn't work otherwise
	var amountString = String(amount).replace(/\$/g,'').replace(/,/g,'');
	var i = parseFloat(amountString);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	
	s = CommaFormatted(s);
	return '$' + s;
}

function CommaFormatted(amount)
{
	var delimiter = ','; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}


//Determines if value passed is a valid date.  Returns true if so.  False if not.
function isDate(testValue) 
{    
	var returnValue = true;
    try 
    {
    	var result = $.datepicker.parseDate('mm/dd/yy', testValue);
    }
    catch (e) 
    {
    	//window.alert(e);
        returnValue = false;
    }
    return returnValue;
}
